import Vue from 'vue'
import Router from 'vue-router'
import { importAll } from '@utils/import-all'

Vue.use(Router)

const routeModules = importAll(require.context('@router/modules', false, /\.js$/))
const routes = routeModules.reduce((finallRoutes, routerModule) => {
	return finallRoutes.concat(
		routerModule.module.map((item) => {
			const camelModuleName = routerModule.camelModuleName
			// 路由格式化，添加文件名为一级路由
			return Object.assign({}, item, {
				path: camelModuleName === 'index' ? item.path : `/${camelModuleName}/${item.path}`
			})
		})
	)
}, [])
// 注入默认配置和路由表
const routerInstance = new Router({
	mode: 'history',
	routes,
	scrollBehavior(to) {
		if (to.hash) {
			return {
				selector: to.hash
			}
		}
	}
})

export default routerInstance
